package com.micro.mic_common.advice;

import com.micro.mic_common.other.BusinessException;
import com.micro.mic_common.other.ResponseData;
import com.micro.mic_common.utils.ResponseDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 处理restController 全局异常
 * @Author yijian
 * @Date 2021/12/28 17:41
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ResponseData exceptionHandler(HttpServletRequest httpServletRequest, Exception e){
        log.error("服务出错："+e.getMessage(),e);
        return ResponseDataUtil.error();
    }

    @ExceptionHandler(value = BusinessException.class)
    public ResponseData businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e){
        return ResponseDataUtil.error(e.getCode(),e.getMsg());
    }
}
